﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Controls;
using Petroleum.UC.Public;
using System.Text.RegularExpressions;
using Petroleum.DB.Model;
using Petroleum.Model;
using Petroleum.Tool;
using HZH_Controls;
using System.Threading;
using HZH_Controls.Forms;
using Newtonsoft.Json;
using System.Data.SQLite;
using Sunny.UI;
using System.Drawing.Imaging;
using System.Windows.Forms.DataVisualization.Charting;

namespace Petroleum.UC
{
    public partial class Step14 : UserControl
    {
        FormMain main = new FormMain();
        List<ProgresPlan> ProgresPlanList { get; set; } = new List<ProgresPlan>();

        public Step14(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            dgv.AutoGenerateColumns = false;
            this.Load += Step14_Load;
        }


        #region ==重写键盘事件==
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //在检测到按Ctrl+V键后，系统无法复制多单元格数据，重写ProcessCmdKey方法，屏蔽系统粘贴事件，使用自定义粘贴事件，在事件中对剪贴板的HTML格式进行处理，获取表数据，更新DataGrid控件内容

            if (keyData == (Keys.V | Keys.Control) && this.dgv.SelectedCells.Count > 0 && !this.dgv.SelectedCells[0].IsInEditMode)
            {
                DGVHelper.CopyData(this.dgv);
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        #endregion

        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="RowIndex"></param>
        /// <param name="CellName"></param>
        /// <returns></returns>
        string GetCell(int RowIndex, string CellName)
        {
            string value = dgv.Rows[RowIndex].Cells[CellName].Value.ToString();
            return string.IsNullOrEmpty(value) ? "" : value;
        }
        double GetCellDesimal(int RowIndex, string CellName)
        {
            string value = dgv.Rows[RowIndex].Cells[CellName].Value.ToString();
            return string.IsNullOrEmpty(value) ? 0 : double.Parse(value);
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step13(main));
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow item in dgv.SelectedRows)
                {
                    if (!item.IsNewRow)
                        dgv.Rows.Remove(item);
                }
                return;
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var casing = main.projects.Where(n => n.TableName == TableNames.ProgresPlanList.ToString()).FirstOrDefault();
            if (casing != null)
            {
                Session.ProgresPlanList = JsonConvert.DeserializeObject<List<ProgresPlan>>(casing.TableData);
                ProgresPlanList = Session.ProgresPlanList;
                AddRow();
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            //SaveData();
            chart1.SaveToImage(SpireDocUtil.ImagesPath + "ChartImg\\line.png", ImageFormat.Png);
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.ProgresPlanList.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.ProgresPlanList.ToString(), JsonConvert.SerializeObject(Session.ProgresPlanList));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.ProgresPlanList.ToString(), JsonConvert.SerializeObject(Session.ProgresPlanList), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        void SaveData()
        {
            ProgresPlanList.Clear();
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                var val = dgv.Rows[i].Cells["AccDay"].Value;
                if (val == null || val == "")
                    continue;
                ProgresPlan trajectory = new ProgresPlan()
                {
                    Item = GetCell(i, "Item"),
                    Day = GetCellDesimal(i, "Day"),
                    AccDay = GetCellDesimal(i, "AccDay"),
                    Interval = GetCell(i, "Interval"),
                    IntervalTo = GetCellDesimal(i, "IntervalTo"),
                };
                ProgresPlanList.Add(trajectory);
            }
            Session.ProgresPlanList = ProgresPlanList;
        }
        void AddRow()
        {
            this.dgv.Rows.Clear();
            foreach (var p in ProgresPlanList)
            {
                int index = this.dgv.Rows.Add();
                this.dgv.Rows[index].Cells[0].Value = p.Item;
                this.dgv.Rows[index].Cells[1].Value = p.Day;
                this.dgv.Rows[index].Cells[2].Value = p.AccDay;
                this.dgv.Rows[index].Cells[3].Value = p.Interval;
                this.dgv.Rows[index].Cells[4].Value = p.IntervalTo;
            }
        }
        List<double> listx = new List<double>();
        List<double> listy = new List<double>();
        private void btnexport_BtnClick(object sender, EventArgs e)
        {
            SaveData();
            //chart1.Series.Clear();
            //chart1.Series.dd("Time Depth Curve");
            //chart1.Series[0].ChartType = SeriesChartType.Line;
            //chart1.Series[0].YValueType = ChartValueType.Double;
            //chart1.Series[0].XValueType = ChartValueType.Double;
            //chart1.Series[0].XAxisType = AxisType.Secondary;
            // 设置线的颜色
            //chart1.Series[0].Color = Color.Blue;
            //chart1.ChartAreas[0].AxisX.Interval = 5;
            //chart1.ChartAreas[0].AxisY.IsReversed = true;
            //chart1.ChartAreas[0].AxisX.IntervalOffset = 1.00D;
            // 数据源至界面
            if (ProgresPlanList.Count > 0)
            {
                listx.Clear();
                listy.Clear();
                foreach (var item in ProgresPlanList)
                {
                    listx.Add(item.AccDay);
                    listy.Add(item.IntervalTo);
                }
                chart1.Series[0].Points.Clear();
                chart1.Series[0].Points.DataBindXY(listx, listy);
                //listx.Add(0);
                //chart1.SaveToImage(SpireDocUtil.ImagesPath + "ChartImg\\line.png", ImageFormat.Png);
                //chart1.Series[0].Points.DataBindY(listy);
            }
            else
                FrmTips.ShowTipsWarning(new FormMain(), "没有要生成报表的数据！");
        }
        private void Step14_Load(object sender, EventArgs e)
        {
        }
        private void LineChart_PointValue(object sender, UILineSelectPoint[] points)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var point in points)
            {
                sb.Append(point.Name + ", " + point.Index + ", " + point.X + ", " + point.Y);
            }
            //Console.WriteLine(sb.ToString());
        }
    }
}
